﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;

namespace StorageManage.Logic
{
    /// <summary>
    /// getTree 的摘要说明
    /// </summary>
    public class getTree : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            List<BD_Tree> list=DALSession.Current.BD_TreeDAL.GetListBy(o => o.fatherId==0);
            List<TreeNode> treeNodeList = new List<TreeNode>();
            list.ForEach(o=>{
                TreeNode tree1 = new TreeNode() { 
                    id=o.id,
                    state=o.state,
                    iconCls=o.iconCls,
                    Checked=(bool)o.Checked,
                    text=o.text,
                    attributes=o.attributes
                };
                var childList=DALSession.Current.BD_TreeDAL.GetListBy(t => t.fatherId == o.id);
                List<TreeNode> childrenList = new List<TreeNode>();
                childList.ForEach(p => {
                    TreeNode tree2 = new TreeNode()
                    {
                        id = p.id,
                        state = p.state,
                        iconCls = p.iconCls,
                        Checked = (bool)o.Checked,
                        text = p.text,
                        attributes=p.attributes,

                    };
                    childrenList.Add(tree2);
                });
                tree1.children = childrenList;
                treeNodeList.Add(tree1);
            });
            string json = JsonConvert.SerializeObject(treeNodeList);
            context.Response.Write(json);
   

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}